<template>
  <div class="search-result-container">
    <!-- 点击实现后退效果 -->
    <van-nav-bar
      title="搜索结果"
      left-arrow
      @click-left="$router.go(-1)"
      fixed
    />

    <!-- 搜索列表结果 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      offset="50"
      :immediate-check="false"
    >
      <ArticleItem
        v-for="obj in articleList"
        :key="obj.art_id"
        :item="obj"
        :closeShow="false"
      ></ArticleItem>
    </van-list>
  </div>
</template>

<script>
import ArticleItem from '@/components/ArticleItem.vue'
import { searchResultListAPI } from '@/api'
import { toTimeAgo } from '@/utils/date'
export default {
  name: 'SearchResult',
  components: {
    ArticleItem
  },
  data () {
    return {
      articleList: [], // 文章类表
      loading: false,
      finished: false,
      page: 1 // 搜索结果页码
    }
  },
  methods: {
    onLoad () {
      this.page++
      this.getResultList()
    },
    async getResultList () {
      const res = await searchResultListAPI({
        q: this.$route.params.keywords,
        page: this.page
      })

      if (res.data.data.results.length === 0) {
        this.finished = true
        return
      }
      res.data.data.results.forEach(obj => {
        obj.pubdate = toTimeAgo(obj.pubdate)
      })

      console.log(res)
      this.articleList = [...this.articleList, ...res.data.data.results]
      this.loading = false
    }
  },
  created () {
    this.getResultList()
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
